package com.achun.ximalayamvvm

import android.content.Intent
import android.graphics.Rect
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.achun.ximalayamvvm.BaseApplication.Companion.context
import com.achun.ximalayamvvm.databinding.ActivityRecommendListDetailBinding
import com.achun.ximalayamvvm.databinding.FragmentRecommendListBinding
import com.achun.ximalayamvvm.utils.LogUtil
import com.achun.ximalayamvvm.utils.Uiloader
import com.achun.ximalayamvvm.view.adapter.DetailListAdapter
import com.achun.ximalayamvvm.vm.PlayerViewModel
import com.achun.ximalayamvvm.vm.RecommendDetailViewModel
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.ximalaya.ting.android.opensdk.model.album.Album
import com.ximalaya.ting.android.opensdk.model.track.Track
import jp.wasabeef.glide.transformations.BlurTransformation

class RecommendListDetailActivity : AppCompatActivity() {
   private lateinit var SuccessViewBind:FragmentRecommendListBinding
    private lateinit var detailListAdapter: DetailListAdapter
    private var currenttracks:List<Track>?=null
    private lateinit var currentAlbum:Album
    private var isSubsc=false


    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        var contentView = DataBindingUtil.setContentView<ActivityRecommendListDetailBinding>(
            this,
            R.layout.activity_recommend_list_detail
        )
        initView(contentView)
        initViewModel(contentView)

    }

    @RequiresApi(Build.VERSION_CODES.P)
    private fun initEvent(
        contentView: ActivityRecommendListDetailBinding,
        playerViewModel: PlayerViewModel,
        viewModel: RecommendDetailViewModel
    ) {
        //订阅事件点击
        contentView.subscribeBtn.setOnClickListener{
            if (isSubsc){
                viewModel.deleteSub(currentAlbum)
            }else{
                viewModel.addSub(currentAlbum)
            }

        }
        //列表点击事件
        detailListAdapter.setOnItemClickListerter(object :DetailListAdapter.OnItemCLickListenter{
            override fun onclick(position: Int) {
                LogUtil.e(this,"你点击了第$position 个")
                var instace = PlayerViewModel.instace
                instace.setTrack(currenttracks!!,position)
                var intent = Intent(context, PlayerActivity::class.java)
                startActivity(intent)
            }

        })
        //播放暂停点击
        contentView.playOrPause?.setOnClickListener() {
            var isPlay = playerViewModel.playorPause()
            com.achun.ximalayamvvm.utils.LogUtil.e(this, "音乐是否播放--$isPlay")
            if (isPlay)
                contentView.playOrPause.setImageResource(R.drawable.selecter_pause_btn)
            else
                contentView.playOrPause.setImageResource(R.drawable.selecter_play_btn)
        }


    }

    private fun initView(contentView: ActivityRecommendListDetailBinding) {
        //设置跑马灯，要点击
        contentView.detailPlaymusicTv.isSelected=true
        contentView.detailNullFrame.addView(uiloader)
    }

    private var uiloader:Uiloader= object : Uiloader(context!!) {
        override fun retryOnclick() {

        }

        override fun getSuccessView(): View? {
            return createSuccessView()
        }

    }
    @RequiresApi(Build.VERSION_CODES.P)
    private fun initViewModel(contentView: ActivityRecommendListDetailBinding) {
      var viewModel = RecommendDetailViewModel.instance
        //加载列表数据
        viewModel.loadData()
        uiloader.updataStatus(Uiloader.UISTATUS.LOADING)
        //观察是否已订阅
        viewModel.getIsSubsc().observe(this) {
            if (it){
                contentView.subscribeBtn.text = "已订阅"

                isSubsc=true
            }else{
                contentView.subscribeBtn.text = "+订阅"

                isSubsc=false
            }

        }
        //观察页面标题数据
        viewModel.getalbum().observe(this) {
            currentAlbum=it
            //加载是否已经订阅
            viewModel.isSubscribed(it)
            contentView.run {
                Glide.with(context!!).load(it.coverUrlLarge).apply(RequestOptions.bitmapTransform(BlurTransformation(25,3))).into(detailBackIv)
                Glide.with(context!!).load(it.coverUrlSmall).into(detailCoverIv)
                detailTitleTv.text = it.albumTitle
                detailIntroTv.text = it.albumIntro
            }
        }
        viewModel.gettrackList().observe(this, Observer {
            currenttracks=it
            uiloader.updataStatus(Uiloader.UISTATUS.SUCCESS)
            LogUtil.e(this,"更新数据--")
            detailListAdapter.setDatas(it)


        })
        //播放状态viewModel
        var playerViewModel = PlayerViewModel.instace
        //监听当前播放歌曲
        playerViewModel.getTrack().run {
            observeForever() {
                //更新当前页面
                com.achun.ximalayamvvm.utils.LogUtil.e(this, "更新页面")
                contentView?.run {
                    detailPlaymusicTv.text = it.trackTitle
                }
            }
        }
        //监听播放状态
        playerViewModel.getPlayStatus().run {
            observeForever() {
                if (it) {
                    contentView!!.playOrPause.setImageResource(R.drawable.selecter_pause_btn)


                } else {
                    contentView!!.playOrPause.setImageResource(R.drawable.selecter_play_btn)

                }
            }
        }
        //初始化事件
        initEvent(contentView,playerViewModel,viewModel)

    }

    private fun createSuccessView(): View? {
        SuccessViewBind =
        DataBindingUtil.inflate<FragmentRecommendListBinding>(LayoutInflater.from(context),R.layout.fragment_recommend_list,null,false)
        SuccessViewBind.detailList.run {
            layoutManager=LinearLayoutManager(context)
            detailListAdapter= DetailListAdapter()
            adapter=detailListAdapter
            this.addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    itemPosition: Int,
                    parent: RecyclerView
                ) {
                    outRect.run {
                        left=20
                        top=20
                        right=20
                        bottom=20
                    }
                }
            })

        }
        return SuccessViewBind.root
    }

    override fun onDestroy() {
        super.onDestroy()

    }

    override fun onStop() {
        super.onStop()
        LogUtil.e(this,"onstop0")
    }
}